# coding=utf-8
import time
from function_utils import Global, PsUtil


def cpu_info(interval=2):
    start, cpus = [], []
    for cpu in PsUtil.cpu_times():
        total = sum(cpu)
        idle = cpu[3]
        start.append((total, idle))
    time.sleep(interval)
    for i, cpu in enumerate(PsUtil.cpu_times()):
        total = sum(cpu)
        idle = cpu[3]
        percent = (1 - (idle - start[i][1]) / float(total - start[i][0])) * 100
        if i == 0:
            cpus.append(['{0:7}'.format('CPUs'), int(percent)])
        else:
            cpus.append(['{0:7}'.format('CPU-%s' % i), int(percent)])
    return cpus


def write_widgets_value(value):
    data = [
        {"MultiScaleTab": value},
        {}
    ]
    Global.values_write(data)
    Global.report_info(100)


def main():
    write_widgets_value(cpu_info())


if __name__ == '__main__':
    main()

